refactor(gateway): extract shared streaming generator utility#930
Merged
refactor(gateway): extract shared streaming generator utility#930
Conversation
Extract duplicated SSE streaming logic from chat.py and messages.py into a shared streaming_generator() in gateway/streaming.py. Both endpoints now use the same generator with protocol-specific format/extract/callback functions, ensuring consistent error handling and usage tracking. Fixes #919 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 🚀 New features to boost your workflow:
|
- Add return type annotations to async generator helpers in test_streaming_generator.py (mypy no-untyped-def) - Extract exception message strings to module-level constants (ruff EM101/TRY003) - Update test_streaming_error_event.py to expect HTTP 500 for stream creation failures, matching the refactored behavior where acompletion() is called before the streaming generator starts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Extracts the duplicated SSE streaming logic from
routes/chat.pyandroutes/messages.pyinto a sharedstreaming_generator()ingateway/streaming.py.Both the OpenAI chat completions and Anthropic messages endpoints had structurally identical streaming generators (~50-60 lines each) handling: chunk iteration, usage accumulation, done markers, error SSE events, and usage logging. The only differences were protocol-specific formatting details.
The new shared generator accepts protocol-specific callbacks (format_chunk, extract_usage, on_complete, on_error) and a
StreamFormatdataclass that captures the done marker, error payload, and whether to yield a done marker after errors. Pre-builtOPENAI_STREAM_FORMATandANTHROPIC_STREAM_FORMATconstants are provided.PR Type
Relevant issues
Fixes #919
Checklist
AI Usage Information
AI Model used: Claude Opus 4.6
AI Developer Tool used: Claude Code
Any other info you'd like to share: Identified and implemented as part of a gateway codebase improvement sweep.
I am an AI Agent filling out this form (check box if true)